/*
 * Copyright (C) 2009 Jiannan Lu
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.restsql.db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Jiann.lu@yahoo.com
 */
public class DerbyTableInfo extends GenericTableInfo implements TableInfo{

    DerbyTableInfo(Database db) throws SQLException {
         /* set resultset */
        final String sql =  "select tablename from SYS.SYSTABLES join SYS.SYSSCHEMAS on SYS.SYSTABLES.schemaid=SYS.SYSSCHEMAS.schemaid " +
                    "where SYS.SYSTABLES.TABLETYPE='T' and SYS.SYSSCHEMAS.schemaname= ? ";
        PreparedStatement ps = db.getPreparedStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);//Create prepared statement
        ps.setString(1, db.getSchemaName());//set schema
        ResultSet rs = ps.executeQuery();
        /* set index */
        
        int i = 0;//initialize internal id
        while (rs.next()) {//iterate rs
            //System.out.println(rs.getString("tablename"));
            index.put(rs.getString("tablename"), Integer.valueOf(i));//put column name, internal id to index
            i++;//increase internal id (step 1)
        }
        /* close resultset */
        rs.close();
        ps.close();
    }

}
